"use strict";
const semver = require("semver");
const colors = require("colors");
const log = require("@unluna/log");
const LOWEST_NODE_VERSION = "16.0.0";

class Command {
	constructor(args) {
		this.initArgs(args);
		let runner = new Promise((resolve, reject) => {
			let chain = Promise.resolve();
			chain = chain.then(() => this.checkNodeVersion());
			chain = chain.then(() => this.init());
			chain = chain.then(() => this.exec());
			chain.catch(e => {
				log.error(e.message);
			});
		});
	}

	checkNodeVersion() {
		// 获取当前Node版本号
		const currentVersion = process.version;
		const lowestVersion = LOWEST_NODE_VERSION;
		console.log(`当前node版本：${currentVersion}`.blue);
		// 比对版本号
		if (!semver.gte(currentVersion, lowestVersion)) {
			throw new Error(colors.red(`需要安装${lowestVersion}以上的node版本！ `));
		}
	};

	initArgs(args) {
		if (!args) {
			throw new Error("参数不能为空！");
		}
		if (!Array.isArray(args)) {
			throw new Error("参数必须是数组！");
		}
		if (!args.length) {
			throw new Error("参数为空！");
		}
		this.cmd = args[args.length - 1];
		this._args = args.slice(0, args.length - 1);
	}

	init() {
		throw new Error("init必须实现！");
	}

	exec() {
		throw new Error("exec必须实现！");
	}
}

module.exports = Command;
